<?php

class Client_controller extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('services_model');
        $this->form_validation->set_error_delimiters('', ' ');
    }

   function dashboard(){
       
       $ch = curl_init(SERVER_URL . "services/dashboard");
       //curl_setopt($ch, CURLOPT_POST, 1);
       curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
       curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
       //curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
       
       
       curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
       //echo curl_exec($ch);
       $response = json_decode(curl_exec($ch));
       curl_close($ch);
       
       if ($response->status === OK_STATUS) {
           $this->load->view('dashboard');
       }else{
           $this->load->view('information', array('status' => ERROR_STATUS, 'message' => 'Not logged in'));
       }
       //$this->load->view('dashboard');
   }

    /*
     * author: Lidier
     * purpose: Create a new user according to the information provided
     * parameters:
     *
     */
    function user_reg() {
        if ($this->input->post()) {
            if ($this->form_validation->run()) {
                $ch = curl_init(SERVER_URL . "services/user_reg");
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $this->input->post());
                $response = json_decode(curl_exec($ch));
                curl_close($ch);
                if ($response->status === OK_STATUS) {
                    $this->load->view('user_reg', array('status' => OK_STATUS, 'message' => 'User registered correctly.'));
                } else {
                    $this->load->view('user_reg', $response);
                }
            } else {
                $this->load->view('user_reg', array('status' => ERROR_STATUS, 'message' => 'There were validation errors.'));
            }
        } else {
            $this->load->view('user_reg');
        }
    }
    
    
    /*
     * author: Lidier
     * purpose: Authenticate an user an create the session data for him
     * parameters:
     *
     */
    function login() {
        if ($this->input->post()) {
            if ($this->form_validation->run()) {
                $ch = curl_init(SERVER_URL . "services/login");
                curl_setopt($ch, CURLOPT_POST, 1);
                //curl_setopt($ch, CURLOPT_HEADER_OUT, 1);
                //curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $this->input->post());
                curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
                echo curl_exec($ch);
                $response = json_decode(curl_exec($ch));
                //curl_close($ch);
                
                //$this->load->library('proxy');
                //$json_var = $this->proxy->site(SERVER_URL . "services/login");
                //$response = json_decode($json_var);
                
                if ($response->status === OK_STATUS) {
                    redirect('client_controller/dashboard');
                } else {
                    $this->load->view('login', $response);
                }
            } else {
                $this->load->view('login', array('status' => ERROR_STATUS, 'message' => 'There were validations errors.'));
            }
        } else {
            $this->load->view('login');
        }
    }
    
    
   
    
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */

    function get_products_from_store($store_id = null) {
        if ($store_id !== null) {
                $ch = curl_init(SERVER_URL . "services/get_products_from_store");
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, array('store_id' => $store_id));
                $response = json_decode(curl_exec($ch));
                curl_close($ch);
                if ($response->status === OK_STATUS) {
                    $this->load->view('get_products_from_store', array('status' => OK_STATUS, 'data' => $response->response, 'store_id' => $store_id));
                } else {
                    $this->load->view('information', array('status' => ERROR_STATUS, 'message' => 'There was an error in the curl server call.'));
                }
        } else {
            $this->load->view('information', array('status' => ERROR_STATUS, 'message' => 'Store ID not provided.'));
        }
    }
    
    
    function get_products_from_store_public($store_id = null) {
        if ($store_id !== null) {
                $ch = curl_init(SERVER_URL . "services/get_products_from_store");
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, array('store_id' => $store_id));
                $response = json_decode(curl_exec($ch));
                curl_close($ch);
                if ($response->status === OK_STATUS) {
                    $this->load->view('get_products_from_store_public', array('status' => OK_STATUS, 'data' => $response->response, 'store_id' => $store_id));
                } else {
                    $this->load->view('information', array('status' => ERROR_STATUS, 'message' => 'There was an error in the curl server call.'));
                }
        } else {
            $this->load->view('information', array('status' => ERROR_STATUS, 'message' => 'Store ID not provided.'));
        }
    }
    
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function get_all_stores() {
        $ch = curl_init(SERVER_URL . "services/get_all_stores");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->input->post());
        $response = json_decode(curl_exec($ch));
        curl_close($ch);
        if ($response->status === OK_STATUS) {
            $this->load->view('get_all_stores', array('status' => OK_STATUS, 'data' => $response->response));
        } else {
            $this->load->view('information', array('status' => ERROR_STATUS, 'message' => $response->response));
        }
    }
    
    //*****************************************************************************
    // LOGIN REQUIRED FOR ALL THESE!!
     /*
     * author: Lidier
     * purpose: Destroy all the session data of the currently logged in user
     * parameters:
     *
     */
    function logout(){
        $ch = curl_init(SERVER_URL ."services/logout");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->input->post());
        $response = json_decode(curl_exec($ch));
        curl_close($ch);
        if ($response->status === OK_STATUS) {
            $this->load->view('login');
        } else {
            $this->load->view('login', array('status' => ERROR_STATUS, 'message' => $response->response));
        }
    }
    
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function store_reg(){
        if ($this->input->post()) {
            if ($this->form_validation->run()) {
                $ch = curl_init(SERVER_URL ."services/store_reg");
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $this->input->post());
                //$res = curl_exec($ch);
                // echo $res;
                $response = json_decode(curl_exec($ch));
                curl_close($ch);
                if ($response->status === OK_STATUS) {
                    $this->load->view('information', array('status' => OK_STATUS, 'message' => 'Store registered correctly.'));
                } else {
                    $this->load->view('information', array('status' => ERROR_STATUS, 'message' => $response->response));
                }
            } else {
                $this->load->view('store_reg', array('status' => ERROR_STATUS, 'message' => 'There were validation errors.'));
            }
        } else {
            $this->load->view('store_reg');
        }
    }
    
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function store_edit(){
       if ($this->input->post()) {
            if ($this->form_validation->run()) {
                $ch = curl_init(SERVER_URL . "services/store_edit");
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $this->input->post());
                $response = json_decode(curl_exec($ch));
                curl_close($ch);
                if ($response['status'] === OK_STATUS) {
                    $this->load->view('information', array('status' => OK_STATUS, 'message' => 'Store edited correctly.'));
                } else {
                    $this->load->view('information', array('status' => ERROR_STATUS, 'message' => $response->response));
                }
            } else {
                $this->load->view('store_reg', array('status' => ERROR_STATUS, 'message' => 'There were validation errors.'));
            }
        } else {
            $this->load->view('store_reg');
        }
    }
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function store_del($store_id = null){
          if ($store_id !== null) {
            
                $ch = curl_init(SERVER_URL ."services/store_del");
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, array('id' => $store_id));
                $response = json_decode(curl_exec($ch));
                curl_close($ch);
                if ($response->status === OK_STATUS) {
                    $this->load->view('information', array('status' => OK_STATUS, 'message' => 'Store deleted correctly.'));
                } else {
                    $this->load->view('information', array('status' => ERROR_STATUS, 'message' => $response->response));
                }
            
        } else {
            $this->load->view('information', array('status' => ERROR_STATUS, 'message' => 'Store ID not provided.'));
        }
    }
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function get_my_stores(){
        $ch = curl_init(SERVER_URL . "services/get_my_stores");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->input->post());
        $response = json_decode(curl_exec($ch));
        curl_close($ch);
        if ($response->status === OK_STATUS) {
            $this->load->view('get_my_stores', array('status' => OK_STATUS, 'data' => $response->response));
        } else {
            $this->load->view('information', array('status' => ERROR_STATUS, 'message' => $response->response));
        }
    }
    
    
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function product_add($store_id = null){
        if ($this->input->post()) {
            if ($this->form_validation->run()) {
                $ch = curl_init(SERVER_URL ."services/product_add");
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $this->input->post());
                $response = json_decode(curl_exec($ch));
                curl_close($ch);
                if ($response->status === OK_STATUS) {
                    $this->load->view('information', array('status' => OK_STATUS, 'message' => 'Product registered correctly.'));
                } else {
                    $this->load->view('information', array('status' => ERROR_STATUS, 'message' => $response->response));
                }
            } else {
                $this->load->view('product_add', array('status' => ERROR_STATUS, 'message' => 'There were validation errors.', 'store_id' => $this->input->post('store_id')));
            }
        } else {
            $this->load->view('product_add',array('store_id'=> $store_id));
        }
    }
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function product_edit(){
        if ($this->input->post()) {
            if ($this->form_validation->run()) {
                $ch = curl_init(SERVER_URL . "services/product_edit");
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $this->input->post());
                $response = json_decode(curl_exec($ch));
                curl_close($ch);
                if ($response['status'] === OK_STATUS) {
                    $this->load->view('information', array('status' => OK_STATUS, 'message' => 'Product edited correctly.'));
                } else {
                    $this->load->view('information', array('status' => ERROR_STATUS, 'message' => $response->response));
                }
            } else {
                $this->load->view('product_edit', array('status' => ERROR_STATUS, 'message' => 'There were validation errors.'));
            }
        } else {
            $this->load->view('product_edit');
        }
    }
    
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function product_del($product_id = null){
       if ($product_id !== null) {  
                $ch = curl_init(SERVER_URL . "services/product_del");
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, array('id' => $product_id));
                $response = json_decode(curl_exec($ch));
                curl_close($ch);
                if ($response->status === OK_STATUS) {
                    $this->load->view('information', array('status' => OK_STATUS, 'message' => 'Product deleted correctly.'));
                } else {
                    $this->load->view('information', array('status' => ERROR_STATUS, 'message' => $response->response));
                }
           
        } else {
            $this->load->view('information', array('status' => ERROR_STATUS, 'message' => 'Product ID not provided.'));
        }
    }
  
  
  
    //*****************************************************************************
    //*****************************************************************************
    
    
    /*
     * author: Lidier
     * purpose: Get the basic data of a user
     * parameters:
     *
     */
    function user_get() 
    {
        
    }
    /*
     * author: Lidier
     * purpose: Edit the basic data of a user
     * parameters:
     *
     */
    function user_edit() 
    {
       
    }
    
   
    /*
     * author: Lidier
     * purpose: Disable a single user (set its state to USER_INACTIVE)
     * parameters:
     *
     */
    function user_disable() {
      
    }
    
    /*
     * author: Lidier
     * purpose: Get all the users and their information within a JSON format
     *
     */
    function users_get(){
        
    }
    

    
//*******************************************************************************************************************************
// HIDDEN FUNCTIONS
    
    /*
     * author: Lidier
     * purpose: 
     * parameters: 
     *
     */
    function _is_user_logged()
    {
        if($this->session->userdata('user_id'))
            return true;
        else
            return false;
    }
    

    
    /*
     * author: Lidier
     * purpose: Saves in the session the information provided 
     * parameters: $data -> could be a single string argument or an array containing several information
     */
    function _create_user_session($data){
        if (is_string($data))
            $this->session->set_userdata('user_id', $data['user_id']);
        elseif (is_array($data)){
            $this->session->set_userdata($data);
        }
    }
    
}

/* End of file services.php */
/* Location: ./application/controllers/client_controller.php */